home *** CD-ROM | disk | FTP | other *** search
- Path: gate.compart.fi!usenet
- From: joonas.kervinen@pcb.compart.fi (joonas kervinen)
- Newsgroups: comp.lang.c
- Subject: Is this somekind of a stack fault or what?
- Date: Thu, 11 Jan 1996 20:14:40 GMT
- Organization: Compart BBS, Helsinki, Finland
- Message-ID: <4d2oi6$ghf@gate.compart.fi>
- NNTP-Posting-Host: spider.compart.fi
- X-Newsreader: Forte Free Agent 1.0.82
-
- I have a function that causes a bizarre problem. It's used in a
- Windows program and a DOS version compiled with BCC 4.53 large model.
- Everything is just straight C. I'm pretty sure this function is the
- culprit since slight modifications to it (as described in the source)
- make everything go all right.
-
- The function purpose is to round() a number to certain decimal
- accuracy. It is passed the number as a char pointer (char *sa2) and
- the desired decimal count (deci). While the function is not foolproof
- (it won't handle negative decimals or other clear errors) it is
- sufficient for me. I've found out that sprintf() are not reliable in
- rounding up situation (do a sprintf(mystr,"%f.2d",1.499) or something
- like that and you'll get errors)
-
- I can't figure why the func bombs stack or whatever but it seems to
- overwrite memory or unbalance the stack. When I do a
- roundx(" 9.99 ",1)
- I do get a pointer to "10.0". The problem is that after that strange
- things happen to the rest of my program. Not a big crash but some
- memory or pointer go away. I've carefully examined the func under
- Turbo debugger and it just doesn't overwrite memory. I tend to think
- that there is something wrong with this:
-
- char *roundx(char *sa2, int deci)
- {
- /* */
- static char sa1=" ";
- /* rest of program */
- return sa1;
- }
-
- When I change it to:
- char *roundx(char *sa2, int deci)
- {
- /* */
- char sa1[20];
- /* rest of program with one modification*/
- return sa1;
- }
- ... everything works on DOS and Windows version.
-
- Please tell me what's going on. Please answer via E-mail since I don't
- come to comp.lang.c often (this is also on Windows programming
- section)
-
- OK. Here's the function:
-
- char *roundx(char *sa2, int deci)
- {
- int i, pit, up;
- static char sa1=" ";
- //static char sa1[20]; // comment above line and uncomment this
-
- // and the func will work.
-
- /* find start, decimal point and dot "." */
- i = 0;
- while (1)
- {
- if (sa2[i] != ' ')
- break;
- i++;
- }
- pit = 0;
- up = 1;
- //sa1[0]=' '; //if you make corrections at the beginning
- //uncomment this line
- while (1)
- {
- if (sa2[i] == '.')
- pit = up;
- else
- if (sa2[i] == 0)
- break;
- sa1[up] = sa2[i];
- i++;
- up++;
- }
- if (pit == 0)
- { /* desimaali ja nollat puuttuvat */
- sa1[up] = '.';
- memset(&sa1[up + 1], '0', deci + 1);
- pit = up;
- }
- else
- if (up - pit - 1 < deci + 1)
- { /* Laitetaan nollia perään */
- memset(&sa1[up], '0', deci + 1);
- }
- i = pit + deci;
- if (sa1[i + 1] - 48 > 4)
- up = 1;
- else
- up = 0;
- while (1)
- {
- if (sa1[i] == '.')
- {
- i--;
- continue;
- }
- else
- {
- if (sa1[i] - 48 + up > 9)
- {
- sa1[i] = '0';
- i--;
- continue;
- }
- else
- {
- if (sa1[i] == ' ' && up)
- {
- sa1[i] = '1';
- break;
- }
- else
- if (sa1[i] == '-' && up)
- {
- sa1[i] = '1';
- sa1[i - 1] = '-';
- break;
- }
- sa1[i] = sa1[i] + up;
- break;
- }
- }
- }
- if (deci == 0)
- sa1[pit] = 0;
- else
- sa1[pit + 1 + deci] = 0;
- return sa1;
- }
-
-
-